//
//  LLView.swift
//  LLTryFightingApp
//
//  Created by jinbang.li on 2024/9/20.
//

import UIKit
/**
 // 创建 RingView 实例
         let ringView = RingView(frame: CGRect(x: 0, y: 0, width: 200, height: 200))
         ringView.center = view.center
         ringView.backgroundColor = .clear // 背景色设置为透明
         view.addSubview(ringView)
 
 // 创建 GradientRingView 实例
        let gradientRingView = GradientRingView(frame: CGRect(x: 0, y: 0, width: 200, height: 200))
        gradientRingView.center = view.center
        gradientRingView.backgroundColor = .clear
        view.addSubview(gradientRingView)
 // 创建 ArcView 实例
 let arcView = ArcView(frame: CGRect(x: 0, y: 0, width: 200, height: 200))
 arcView.backgroundColor = .clear
 arcView.center = view.center
 view.addSubview(arcView)

 // 添加旋转动画
 let rotationAnimation = CABasicAnimation(keyPath: "transform.rotation.z")
 rotationAnimation.fromValue = 0
 rotationAnimation.toValue = 2 * Double.pi
 rotationAnimation.duration = 2
 rotationAnimation.repeatCount = .infinity

 arcView.layer.add(rotationAnimation, forKey: "rotationAnimation")
 */
class ArcView: UIView {
    
    override func draw(_ rect: CGRect) {
        super.draw(rect)
        
        // 获取当前绘图上下文
        guard let context = UIGraphicsGetCurrentContext() else { return }
        
        // 定义圆弧的参数
        let center = CGPoint(x: bounds.midX, y: bounds.midY)
        let radius: CGFloat = min(bounds.width, bounds.height) / 2 - 7
        let startAngle: CGFloat = 0
        let endAngle: CGFloat = .pi // 半圆
        
        // 绘制圆弧
        context.setStrokeColor(UIColor.red.cgColor)
        context.setLineWidth(5)
        context.addArc(center: center, radius: radius, startAngle: startAngle, endAngle: endAngle, clockwise: false)
        context.strokePath()
    }
}




class GradientRingView: UIView,CAAnimationDelegate {
    var clickAction: (() -> Void)?
    private let ringWidth: CGFloat = 14.0 // 圆环的宽度
    
    override func layoutSubviews() {
        super.layoutSubviews()
        applyGradient()
    }
    
    private func applyGradient() {
        // 移除之前的渐变层
        layer.sublayers?.filter { $0 is CAGradientLayer }.forEach { $0.removeFromSuperlayer() }
        
        // 创建渐变层
        let gradientLayer = CAGradientLayer()
        gradientLayer.frame = bounds
        gradientLayer.colors = [UIColor.red.cgColor, UIColor.blue.cgColor] // 渐变颜色
        gradientLayer.startPoint = CGPoint(x: 0, y: 0)
        gradientLayer.endPoint = CGPoint(x: 1, y: 1)
        
        // 创建圆环路径
        let circlePath = UIBezierPath(
            arcCenter: CGPoint(x: bounds.midX, y: bounds.midY),
            radius: (bounds.width - ringWidth) / 2,
            startAngle: 0,
            endAngle: 2 * .pi,
            clockwise: true
        )
        
        let maskLayer = CAShapeLayer()
        maskLayer.path = circlePath.cgPath
        maskLayer.strokeColor = UIColor.black.cgColor
        maskLayer.fillColor = UIColor.clear.cgColor
        maskLayer.lineWidth = ringWidth
        maskLayer.lineCap = .round
        
        let animation = CAKeyframeAnimation(keyPath: "strokeEnd")
        animation.delegate = self // 设置代理
        animation.values = [0, 1]
        animation.keyTimes = [0, 1]
        animation.duration = 2.0 // 动画持续时间
        animation.repeatCount = 1 // 无限循环

        // 将动画添加到 CAShapeLayer
        maskLayer.add(animation, forKey: "strokeEndAnimation")
      
        gradientLayer.mask = maskLayer
        layer.addSublayer(gradientLayer)
    }
    func animationDidStart(_ anim: CAAnimation) {
        print("动画开始")
    }
    // CAAnimationDelegate 方法
        func animationDidStop(_ anim: CAAnimation, finished flag: Bool) {
            if flag {
                print("动画完成")
                // 在这里执行你希望在动画结束时做的事情
            }
        }
}



class RingView: UIView {

    var ringColor: UIColor = .gray
    var ringWidth: CGFloat = 14.0

    override func draw(_ rect: CGRect) {
        super.draw(rect)
        
        guard let context = UIGraphicsGetCurrentContext() else { return }
        
        // 计算圆环的圆心和半径
        let center = CGPoint(x: bounds.midX, y: bounds.midY)
        let radius = min(bounds.width, bounds.height) / 2 - ringWidth / 2
        
        // 设置圆环的颜色和宽度
        context.setStrokeColor(ringColor.cgColor)
        context.setLineWidth(ringWidth)
        context.setLineCap(.round) // 使圆环的端点是圆形的

        // 绘制圆环
        context.addArc(center: center, radius: radius, startAngle: 0, endAngle: 2 * .pi, clockwise: false)
        context.strokePath()
    }
}
